<?php

// SQL information
$db_username = "root";
$db_password = "Av5FmC4N6chnByW7";
$db_server = "localhost";
$db_name = "gs";

// Connect
$db = new PDO('mysql:dbname=' . $db_name . ';host=' . $db_server . '', $db_username, $db_password);

// Classes
require_once('../classes/gsFeaturedItems.class.php');
require_once('../classes/gsLkBrand.class.php');
require_once('../classes/gsLkType.class.php');
require_once('../classes/gsProducts.class.php');

/*
 * -----------------------------------------------
 * Lookup Functions
 * -----------------------------------------------
 */

/**
 * Input: type name
 * Output: type ID
 *
 * @param type $strType
 * @return int id the id belonging to that type, otherwise null
 */
function lookup_idByType($strType) {
    global $db;

    $typeObj = new gsLkType();
    $typeObj->setName($strType);
    foreach (gsLkType::findByExample($db, $typeObj) as $member) {
	return $member->getId();
    }
    return null; // error
}

/**
 *
 * Gets the name of a type by ID
 * 
 * @global PDO $db
 * @param type $id
 * @return type 
 */
function lookup_typeById($id) {
    global $db;

    $typeObj = new gsLkType();
    $typeObj->setId($id);
    foreach (gsLkType::findByExample($db, $typeObj) as $member) {
	return $member->getName();
    }
    return null; // error
}


/**
 *
 * Gets the ID of a brand by name
 * 
 * @global PDO $db
 * @param type $strBrand
 * @return type 
 */
function lookup_idByBrand($strBrand) {
    global $db;

    $brandObj = new gsLkBrand();
    $brandObj->setName($strBrand);
    foreach (gsLkBrand::findByExample($db, $brandObj) as $member) {
	return $member->getId();
    }
    return null; // error
}

/**
 *
 * Gets the name of a brand by ID
 * 
 * @global PDO $db
 * @param int $id
 * @return string $type 
 */
function lookup_brandById($id) {
    global $db;

    $brandObj = new gsLkBrand();
    $brandObj->setId($id);
    foreach (gsLkBrand::findByExample($db, $brandObj) as $member) {
	return $member->getName();
    }
    return null; // error
}

/*
 * -----------------------------------------------
 * Non-Lookup Functions
 * -----------------------------------------------
 */

/**
 * returns an array of gsProducts for a particular category
 *
 * @global $db
 * @param string $type The type (category) to filter
 * @param string $brand The brand (manufacturer) to filter
 * @param DSC_Array $sortBy define dsc array for sortby (see: http://code.google.com/p/db2php/wiki/Examples)
 * @return gsProducts[]
 */
function getProducts($type=null, $brand=null, $sortBy=null) {
    global $db;

    $filteringExample = new gsProducts();
    
    if ($type != null) {
	$typeId = lookup_idByType($type);
	if ($typeId == null) {
	    printf("Error: unable to find ID for type");
	    exit(0);
	}	
	$filteringExample->setLkType($typeId);
    }
	    
    if($brand != null) {
	$brandId = lookup_idbyBrand($brand);
	
	if ($brandId == null) {
	    printf("Error: unable to find ID for brand");
	    exit(0);
	}
	$filteringExample->setLkBrand($brandId);
    }
	
    return gsProducts::findByExample($db, $filteringExample, true, $sortBy);
}


/*
 * -----------------------------------------------
 * Tests
 * -----------------------------------------------
 */

// Test 1: test to see if I can retrieve products by category
$products[] = new gsProducts();
$products = getProducts("Oscilloscopes", "HP/Agilent");

for ($i = 0; $i < sizeof($products); $i++) {
    echo $products[$i]->getId() . " -- [" . lookup_brandbyId($products[$i]->getLkBrand()) . "] " .$products[$i]->getModelNo() . "<br>";
    //if($i == 1000) break;
}
// End test
?>
